//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "rcmdapp.h"
#include "mainwnd.h"
#include "listpaircontroller.h"

CListPairController::CListPairController(CPersistenList* left, CPersistenList* right):
	m_left(left), m_right(right)
{
	try
	{
		MACBLOCK leftData  = wxGetApp().GetMainWnd()->GetPropStorage().GetMACList( left->GetData().id  );
		left->SetData( leftData );
	}catch(...) {}

	try
	{
		MACBLOCK rightData = wxGetApp().GetMainWnd()->GetPropStorage().GetMACList( right->GetData().id );
		right->SetData( rightData );
	}catch(...) {}

	CPersistenList* pWhite = left->GetData().id==cMACBlockRuleIdWL?left:right;
	CPersistenList* pBlack = pWhite==left?right:left;

	if ( pWhite->GetData().blockOthers ) pBlack->Enable(false);
}

CListPairController::~CListPairController(void)
{
}

void CListPairController::OnAddItem(CPersistenList* pFrom, MACLIST& listIn)
{
	assert(pFrom);

	if ( !listIn.empty() )
	{
		CPersistenList* pPair= pFrom==m_left?m_right:m_left;
		assert(pPair);

		if ( pFrom->CanAcceptItems(listIn) )
		{
			MACLIST lstNew = pFrom->RemoveExisted(listIn);

			pPair->RemoveItems(listIn);

			if ( pFrom->CanAddItems(lstNew) )
			{
				if ( !lstNew.empty() )
				{
					pFrom->AddItems(lstNew);
				}
			}
		}
	}
}

void CListPairController::OnListChanged(CPersistenList* pFrom)
{
	assert( pFrom );

	CPersistenList* pPair= pFrom==m_left?m_right:m_left;
	assert(pPair);

	MACBLOCK block = pFrom->GetData();
	pPair->Enable( !block.blockOthers );

	wxGetApp().GetMainWnd()->GetPropStorage().SetMACList( block );

	MACBLOCK blockPair = pPair->GetData();

	wxGetApp().GetMainWnd()->GetLogic().ExecuteMACBlock();
}

bool CListPairController::GetItemFromPair(CPersistenList* pFrom, MACENTRY& entry)
{
	bool bRes(false);

	assert( pFrom );

	CPersistenList* pPair= pFrom==m_left?m_right:m_left;
	assert(pPair);

	MACBLOCK block = pPair->GetData();

	for (size_t i=0;i<block.list.size();i++)
	{
		if ( block.list[i].strMAC == entry.strMAC )
		{
			entry = block.list[i];
			bRes = true;
			break;
		}
	}

	return bRes;
}

bool CListPairController::IsExistedInPair( CPersistenList* pFrom,
										  const MACLIST&	list )
{
	bool bRes(false);

	assert( pFrom );

	CPersistenList* pPair= pFrom==m_left?m_right:m_left;
	assert(pPair);

	for (size_t i=0;i<list.size();i++)
	{
		if (  pPair->ExistInList(list[i]) )
		{
			bRes = true;
			break;
		}
	}

	return bRes;
}
